package littlewhisker.components.ui.dial {
	import caurina.transitions.Tweener;

	import littlewhisker.globals.constants.eventtypes.UIEventTypes;

	import flash.events.Event;
	import flash.events.MouseEvent;

	import littlewhisker.components.ui.AbstractUIComponent;

	/**
	 * @author Philip
	 */
	public class AbstractDial extends AbstractUIComponent implements IDialComponent {
		
		private var _rotationSpeed:Number;
		
		private var _minLimit:Number =-180;
		private var _maxLimit:Number = 180;
		
		public function get rotationSpeed() : Number {
			return _rotationSpeed;
		}
		
		public function set rotationSpeed(rotationSpeed : Number) : void {
			_rotationSpeed = rotationSpeed;
		}
		
		public function get minLimit() : Number {
			return _minLimit;
		}
		
		public function set minLimit(degrees : Number) : void {
			_minLimit = degrees;
		}
		
		public function get maxLimit() : Number {
			return _maxLimit;
		}
		
		public function set maxLimit(degrees : Number) : void {
			_maxLimit = degrees;
		}
		
		protected var prevX:Number;
		protected var prevY:Number;
		protected var destination:Number = 0;
		
		public function destory():void{
			removeListeners();	
		}
		
		override protected function enable() : void {
			asset.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
		}
		
		override protected function disable() : void {
			asset.removeEventListener(MouseEvent.MOUSE_DOWN, onClick);
		}
		
		protected function removeListeners() : void {
			asset.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
			asset.stage.removeEventListener(MouseEvent.MOUSE_UP, onRelease);
			asset.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
		}
		
		protected function onClick(e:MouseEvent) : void {
			prevX = asset.stage.mouseX;
			prevY = asset.stage.mouseY;
			asset.stage.addEventListener(MouseEvent.MOUSE_UP, onRelease);
			asset.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
		}
		
		protected function onRelease(e : MouseEvent) : void {			
			asset.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
		}
		
		// Must be overriden
		protected function onMove(event:MouseEvent):void{};
		
		protected function rotate() : void {	
			dispatchEvent(new Event(UIEventTypes.ON_DIAL_CHANGE));
			Tweener.addTween(asset, {time:rotationSpeed, transition:"easeInQuint", rotation:destination, onComplete:doneMove});
		}
		
		protected function doneMove():void{};
	}

}
